package com.rm.zeyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rm.zeyou.entity.User;
import com.rm.zeyou.entity.UserConsumeDetail;
import com.rm.zeyou.mapper.UserConsumeDetailMapper;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.UserConsumeDetailService;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.vo.request.SelectUserConsumptionListVo;
import com.rm.zeyou.vo.response.ConsumptionDetailsList;
import com.rm.zeyou.vo.response.SelectUserConsumptionListResponseBaseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户消费记录表 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-03-12 15:55:09
 */
@Service
public class UserConsumeDetailServiceImpl extends ServiceImpl<UserConsumeDetailMapper, UserConsumeDetail> implements UserConsumeDetailService {
    @Resource
    private UserConsumeDetailMapper userConsumeDetailMapper;

    @Override
    public ApiRestResponse<SelectUserConsumptionListResponseBaseVo> selectUserConsumptionList(User user, SelectUserConsumptionListVo requestVo) {
        SelectUserConsumptionListResponseBaseVo responseVo = new SelectUserConsumptionListResponseBaseVo();
        responseVo.setUserBalance(user.getUserBalance());
        List<ConsumptionDetailsList> consumptionDetailsList = new ArrayList<>();
        PageHelper.startPage(requestVo.getPageNum(), requestVo.getPageSize());
        List<UserConsumeDetail> userConsumeDetails = userConsumeDetailMapper.selectList(new LambdaQueryWrapper<UserConsumeDetail>()
                .eq(UserConsumeDetail::getUserId, user.getId())
                .orderByDesc(UserConsumeDetail::getCreateTime));
        if (CheckEmptyUtil.isEmpty(userConsumeDetails)) {
            responseVo.setVoList(new PageInfo<>());
        } else {
            PageInfo<UserConsumeDetail> source = new PageInfo<>(userConsumeDetails);
            PageInfo<ConsumptionDetailsList> target = new PageInfo<>();
            BeanUtils.copyProperties(source, target);
            source.getList().forEach(item -> {
                ConsumptionDetailsList consumptionDetails = new ConsumptionDetailsList();
                consumptionDetails.setConsumptionPrice(item.getConsumptionPrice());
                consumptionDetails.setConsumptionType(item.getConsumptionType());
                consumptionDetails.setConsumptionDescribe(item.getConsumptionDescribe());
                consumptionDetails.setCreateTime(item.getCreateTime());
                consumptionDetailsList.add(consumptionDetails);
            });

            target.setList(consumptionDetailsList);
            responseVo.setVoList(target);
        }
        return ApiRestResponse.success(responseVo);
    }
}
